//
//  JQFYUserData.m
//  JQFY
//
//  Created by apple on 2017/8/28.
//  Copyright © 2017年 yannis. All rights reserved.
//

#import "JQFYUserData.h"

@implementation JQFYUserData
+(instancetype)sharedData{
    
    static JQFYUserData *instance;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        
        instance = [[self alloc]init];
        
    });
    return instance;
}



/***    归档    ***/
- (void)encodeWithCoder:(NSCoder *)aCoder{
    
    [aCoder encodeBool:self.isLogin forKey:@"isLogin"];
    [aCoder encodeObject:self.password forKey:@"password"];
    [aCoder encodeObject:self.activityId forKey:@"activityId"];
    [aCoder encodeObject:self.customerName forKey:@"customerName"];
    [aCoder encodeInteger:self.userId forKey:@"userId"];
    [aCoder encodeInteger:self.errorCount forKey:@"errorCount"];
    [aCoder encodeObject:self.username forKey:@"username"];
    [aCoder encodeObject:self.loginName forKey:@"loginName"];
    [aCoder encodeObject:self.token forKey:@"token"];
    [aCoder encodeObject:self.customerId forKey:@"customerId"];
    [aCoder encodeObject:self.accessToken forKey:@"accessToken"];
    [aCoder encodeObject:self.alis forKey:@"alis"];
    [aCoder encodeInteger:self.customerType forKey:@"customerType"];
    [aCoder encodeBool:self.isSetting forKey:@"isSetting"];
    [aCoder encodeInteger:self.channelId forKey:@"channelId"];
    [aCoder encodeObject:self.securityCode forKey:@"securityCode"];
    
}

/***    解档    ***/
- (instancetype)initWithCoder:(NSCoder *)aDecoder{
    if (self = [super init]) {
        self.isLogin = [aDecoder decodeBoolForKey:@"isLogin"];
        self.userId = [aDecoder decodeIntegerForKey:@"userId"];
        self.securityCode = [aDecoder decodeObjectForKey:@"securityCode"];
        self.activityId = [aDecoder decodeObjectForKey:@"activityId"];
        self.username = [aDecoder decodeObjectForKey:@"username"];
        self.loginName = [aDecoder decodeObjectForKey:@"loginName"];
        self.password = [aDecoder decodeObjectForKey:@"password"];
        self.customerName = [aDecoder decodeObjectForKey:@"customerName"];
        self.errorCount = [aDecoder decodeIntegerForKey:@"errorCount"];
        self.token = [aDecoder decodeObjectForKey:@"token"];
        self.customerId = [aDecoder decodeObjectForKey:@"customerId"];
        self.accessToken = [aDecoder decodeObjectForKey:@"accessToken"];
        self.alis = [aDecoder decodeObjectForKey:@"alis"];
        self.customerType = [aDecoder decodeIntegerForKey:@"customerType"];
        self.isSetting = [aDecoder decodeBoolForKey:@"isSetting"];
        self.channelId = [aDecoder decodeIntegerForKey:@"channelId"];
    }
    return self;
}

#pragma mark - 保存用户信息
-(void)saveUserInfo{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"userData.archive"];
    [NSKeyedArchiver archiveRootObject:self toFile:path];
}

#pragma mark - 读取用户登录信息
+(JQFYUserData *)userDataInSandbox{
    NSString *path = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES).lastObject stringByAppendingPathComponent:@"userData.archive"];
    JQFYUserData *data = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    return data;
}
//注销清除用户信息
-(void)cleanUserInfo{
    
    self.isLogin = NO;
    self.token = @"";
    
    [self saveUserInfo];
}

@end
